<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Gotcha\App\Extensions;

use GotchaDatabase\GotchaContentPeer;
use GotchaDatabase\GotchaLayersPeer;
use GotchaDatabase\GotchaModulesPeer;
use GotchaDatabase\GotchaThemesQuery;
use Gotcha\App\Types\String;
use GotchaDatabase\GotchaZonesPeer;

// Gotcha themes

final class GotchaThemesExtension {
    static public $view_file_name_handler = '';

    // Current active theme
    private $current_theme_name;
    private static $current_theme_name_static;

    public $adminFilter = false;

    public function __construct() {
        // We load a name of current theme to set further view
        $themes = new GotchaThemesQuery();
        $active_theme = $themes->create()
                                    ->select(array('Strong_Name'))
                                    ->where('Is_Active', true)
                                    ->find();

        $active_theme = new String($active_theme->getCurrent());

        // None of the themes can be activated so we have to
        // take care of this situation by enabling SafeMode
        if($this->adminFilter == false) {
            if(empty($active_theme) || !isset($active_theme) || is_null($active_theme)) {
                $this->current_theme_name = 'SafeMode';
                self::$current_theme_name_static = 'SafeMode';
            } else {
                // We also have to make sure theme dir exists
                if (GotchaFileSystemExtension::CheckDirExistence(new String(GOTCHA_ROOT_DIR.'/Themes/'.$active_theme)) === true) {
                    $this->current_theme_name = $active_theme;
                    self::$current_theme_name_static = $active_theme;
                } else {
                    GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('THEME_DIR_DOESNT_EXIST'))), $active_theme);
                    $this->current_theme_name = 'SafeMode';
                    self::$current_theme_name_static = 'SafeMode';
                }
            }
        } else {
            $this->current_theme_name = 'TheAdmin';
            self::$current_theme_name_static = 'TheAdmin';
        }
    }

    public function GenerateViewLayout(String $view_file_name_handler) {
        // Attach file name handler
        self::$view_file_name_handler = $view_file_name_handler;

        // Require main document file
        if (GotchaFileSystemExtension::CheckFileExistence(new String('Document.phtml'), new String(GOTCHA_ROOT_DIR.'/Themes/'.$this->current_theme_name.'/Views/')) === true) {
            require_once GOTCHA_ROOT_DIR.'/Themes/'.$this->current_theme_name.'/Views/Document.phtml';
        } else {
            if (GotchaFileSystemExtension::CheckFileExistence(new String('Document.phtml'), new String(GOTCHA_ROOT_DIR.'/App/Templates/Layout/')) === true) {
                require_once GOTCHA_ROOT_DIR.'/App/Templates/Layout/Document.phtml';
            } else {
                GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Document.phtml'));
                GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Document.phtml'));
                exit;
            }
        }
    }

    static public function Head() {
        if (GotchaFileSystemExtension::CheckFileExistence(new String('Head.phtml'), new String(GOTCHA_ROOT_DIR.'/Themes/'.self::$current_theme_name_static.'/Views/')) === true) {
            require_once GOTCHA_ROOT_DIR.'/Themes/'.self::$current_theme_name_static.'/Views/Head.phtml';
        } else {
            if (GotchaFileSystemExtension::CheckFileExistence(new String('Head.phtml'), new String('App/Templates/Layout/')) === true) {
                require_once GOTCHA_ROOT_DIR.'/App/Templates/Layout/Head.phtml';
            } else {
                GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Head.phtml'));
                GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Head.phtml'));
                exit;
            }
        }
    }

    static public function Foot() {
        if (GotchaFileSystemExtension::CheckFileExistence(new String('Foot.phtml'), new String(GOTCHA_ROOT_DIR.'/Themes/'.self::$current_theme_name_static.'/Views/')) === true) {
            require_once GOTCHA_ROOT_DIR.'/Themes/'.self::$current_theme_name_static.'/Views/Foot.phtml';
        } else {
            if (GotchaFileSystemExtension::CheckFileExistence(new String('Foot.phtml'), new String('App/Templates/Layout/')) === true) {
                require_once GOTCHA_ROOT_DIR.'/App/Templates/Layout/Foot.phtml';
            } else {
                GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Foot.phtml'));
                GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Foot.phtml'));
                exit;
            }
        }
    }

    static public function Body() {
        if (GotchaFileSystemExtension::CheckFileExistence(new String('Body.phtml'), new String(GOTCHA_ROOT_DIR.'/Themes/'.self::$current_theme_name_static.'/Views/')) === true) {
            require_once GOTCHA_ROOT_DIR.'/Themes/'.self::$current_theme_name_static.'/Views/Body.phtml';
        } else {
            if (GotchaFileSystemExtension::CheckFileExistence(new String('Body.phtml'), new String('App/Templates/Layout/')) === true) {
                require_once GOTCHA_ROOT_DIR.'/App/Templates/Layout/Body.phtml';
            } else {
                GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Body.phtml'));
                GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Body.phtml'));
                exit;
            }
        }
    }

    static public function Content() {
        require_once self::$view_file_name_handler;
    }

    public static function getZoneContent(String $zoneStrongName) {
        // We are loading content for given zone name
        // using current active layer additionally

        // Current alias
        $currentAlias = new GotchaRoutingExtension();

        // We have to find to which layer our zone
        // is connected to
        $layerCriteria = new \Criteria();
        $layerCriteria->add(GotchaLayersPeer::ALIAS, $currentAlias);

        if (GotchaLayersPeer::doCount($layerCriteria) === 0) {
            $layerCriteria->addOr(GotchaLayersPeer::ALIAS, 'Default');
        }

        $layerId = GotchaLayersPeer::doSelectOne($layerCriteria)->getId();

        // Since we have layer's PK we can find content which
        // is connected to our zone
        $zoneCriteria = new \Criteria();
        $zoneCriteria->add(GotchaZonesPeer::GOTCHA_LAYERS_ID, '3');
        $zoneCriteria->addOr(GotchaZonesPeer::GOTCHA_LAYERS_ID, $layerId);
        $zoneCriteria->add(GotchaZonesPeer::STRONG_NAME, $zoneStrongName);

        if (GotchaZonesPeer::doCount($zoneCriteria) === 0) {
            return;
        }

        foreach (GotchaZonesPeer::doSelect($zoneCriteria) as $zoneDoSelect) {
            $zoneContentType = $zoneDoSelect->getContentType();
            $zoneContentName = $zoneDoSelect->getContentStrongName();

            // If content type = HTML we should search it in
            // Gotcha_Content table
            if ($zoneContentType === 'HTML') {

                // Lets search our content type
                $contentCriteria = new \Criteria();
                $contentCriteria->add(GotchaContentPeer::STRONG_NAME, $zoneContentName);

                if (GotchaContentPeer::doCount($contentCriteria) === 0) {
                    continue;
                }

                $contentText = GotchaContentPeer::doSelectOne($contentCriteria)->getText();

                // Start bufor with some HTML structure
                $html = '<div id="zone-'.GotchaContentPeer::doSelectOne($contentCriteria)->getStrongName().'" class="zone">';
                $html .= $contentText;
                $html .= '</div>';

                echo $html;

            // If it is MODULE we load its DefaultController
            // Index() action view
            } else if ($zoneContentType === 'MODULE') {
                // Get strong name
                $moduleCriteria = new \Criteria();
                $moduleCriteria->add(GotchaModulesPeer::STRONG_NAME, $zoneContentName);
                $moduleCriteria->add(GotchaModulesPeer::IS_ACTIVE, 1);

                if (GotchaModulesPeer::doCount($moduleCriteria) === 0) {
                    continue;
                }

                $moduleName = GotchaModulesPeer::doSelectOne($moduleCriteria)->getStrongName();

                if (GotchaFileSystemExtension::CheckFileExistence(new String('Index.phtml'), new String(GOTCHA_ROOT_DIR.'/Modules/'.$moduleName.'/Views/Default/')) === true) {
                    require_once GOTCHA_ROOT_DIR.'/Modules/'.$moduleName.'/Views/Default/Index.phtml';
                } else {
                    GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('MODULE_DEFAULT_VIEW_DOESNT_EXIST'))));
                    GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('MODULE_DEFAULT_VIEW_DOESNT_EXIST'))));
                    exit;
                }
            }
        }
    }

    public static function isNullOrEmptyZone(String $zoneStrongName) {
        // We are checking if current zone is null

        // Current alias
        $currentAlias = new GotchaRoutingExtension();

        // We have to find to which layer our zone
        // is connected to
        $layerCriteria = new \Criteria();
        $layerCriteria->add(GotchaLayersPeer::ALIAS, $currentAlias);

        if (GotchaLayersPeer::doCount($layerCriteria) === 0) {
            $layerCriteria->addOr(GotchaLayersPeer::ALIAS, 'Default');
            return null;
        }

        $layerId = GotchaLayersPeer::doSelectOne($layerCriteria)->getId();

        // Since we have layer's PK we can find content which
        // is connected to our zone
        $zoneCriteria = new \Criteria();
        $zoneCriteria->add(GotchaZonesPeer::GOTCHA_LAYERS_ID, '3');
        $zoneCriteria->addOr(GotchaZonesPeer::GOTCHA_LAYERS_ID, $layerId);
        $zoneCriteria->add(GotchaZonesPeer::STRONG_NAME, $zoneStrongName);

        if (GotchaZonesPeer::doCount($zoneCriteria) === 0) {
            return null;
        }

        // If we get that far we render zone
        return true;
    }
}